<template>
  <pro-layout
    :menus="menus"
    :siderWidth="200"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :breadcrumbRender="breadcrumbRender"
    :rightContentRender="{}"
    :i18nRender="i18nRender"
    v-bind="settings"
  >
    <setting-drawer :settings="settings" @change="handleSettingChange" v-if="!isProPreviewSite"/>
    <!-- 1.0.0+ 版本 pro-layout 提供 API，
          我们推荐使用这种方式进行 LOGO 和 title 自定义
    -->
    <template v-slot:menuHeaderRender>
      <div style="line-height: 45px;height: 45px;">
        <img class="image" src="/logo.jpg" style="height: 30px;width: 30px;">
        <h1>{{ title }}</h1>
      </div>
    </template>
    <template v-slot:rightContentRender>
      <span style="line-height: 45px;font-size: 16px">{{ breadcrumbTitle }}</span>
      <right-content :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile" :theme="settings.theme"/>
    </template>
    <template v-slot:footerRender>
      <global-footer/>
    </template>

    <!-- layout content -->
    <a-layout-content
      v-if="multiTab"
      :style="{ height: '35px', margin: '0 0 0px 10px', paddingTop: settings.fixedHeader ? '45px' : '0' }">
      <div class="kyzMuliTab">
        <multi-tab></multi-tab>
        <transition name="page-transition">
        </transition>
      </div>
    </a-layout-content>
    <div style="padding: 2px 2px 2px 0px; " :style="{height:`${HEIGHT -113 }px`}" >
      <!--    <page-header-wrapper>-->
      <keep-alive :include="cached">
        <router-view class="span-24 c-h-100p c-overflow-auto-y" :key="$route.fullPath"/>
      </keep-alive>
      <div style="position: fixed;margin-left:-13px;z-index:9999999999;background-color: white;bottom: 0;height: 30px;line-height: 30px; border-color: rgb(196,194,194);
width: 100%">
        <div style="margin-left: 10px;font-family: Arial;text-align: center;">顺成科技 版权所有 (推荐谷歌浏览器，获得更快响应速度) 技术支持:山东顺成科技有限公司</div>
      </div>
      <!--    </page-header-wrapper>-->
    </div>
  </pro-layout>
</template>

<script>
import MultiTab from '@/components/MultiTab'
import { SettingDrawer, updateTheme } from '@ant-design-vue/pro-layout'
import { mapState } from 'vuex'
import { CONTENT_WIDTH_TYPE, SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'
import { domTitle } from '@/utils/domUtil'
import { i18nRender } from '@/locales'

import defaultSettings from '@/config/defaultSettings'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'
import LogoSvg from '../assets/logo.svg?inline'

export default {
  name: 'BasicLayout',
  components: {
    SettingDrawer,
    RightContent,
    LogoSvg,
    GlobalFooter,
    MultiTab
  },
  data () {
    return {
      cached: this.$store.state.catchArr,
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      breadcrumbTitle: '欢迎使用智慧运维管理平台',
      title: domTitle,
      multiTab: defaultSettings.multiTab,
      settings: {
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'sidemenu' ? CONTENT_WIDTH_TYPE.Fluid : defaultSettings.contentWidth,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,

        hideHintAlert: false,
        hideCopyButton: false
      },
      // 媒体查询
      query: {},
      HEIGHT: 0,
      // 是否手机模式
      isMobile: false
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters
    })
  },
  created () {
    const routes = this.mainMenu.find(item => item.path === '/')
    this.menus = (routes && routes.children) || []
    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
  },
  mounted () {
    this.HEIGHT = document.body.clientHeight
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    i18nRender,
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
        // this.settings.fixSiderbar = false
      }
    },
    breadcrumbRender ({
                        route,
                        params,
                        routes,
                        paths,
                        h
                      }) {
      // this.breadcrumbTitle = route.breadcrumbName
      return ''
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({
                           type,
                           value
                         }) {
      console.log('type', type, value)
      type && (this.settings[type] = value)

      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
          }
          break
      }
    },
    logoRender () {
      return <LogoSvg/>
    }
  },
  watch: {
    $route: {
      // 监听路由变化
      handler: function (to, from) {
        this.cached = this.$store.state.catchArr
      }
    }
  }
}
</script>

<style lang="less">
@import "./BasicLayout.less";
@import url('./system.css');

//=====================头部样式========================
.ant-pro-global-header-trigger {
  height: 45px !important;
  line-height: 45px !important;
  vertical-align: top;
  padding: 0 22px;
  display: inline-block;
  cursor: pointer;
  transition: all 0.3s, padding 0s;
}
</style>
